<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{total}} 条记录</span>
          <el-button
          @click="openDrawer('add')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round>
            添加面经
          </el-button>
        </div>
      </template>
      <el-table
      :data="list"
      style="width: 100%">
      <el-table-column
        prop="stem"
        label="标题"
        width="350">
      </el-table-column>
      <el-table-column
        prop="creator"
        label="作者"
        width="120"
        >
      </el-table-column>
      <el-table-column
        prop="likeCount"
        label="点赞"
        >
      </el-table-column>
      <el-table-column
        prop="views"
        label="浏览数">
      </el-table-column>
      <el-table-column
        prop="createdAt"
        label="更新时间"
        width="180">
      </el-table-column>
       <el-table-column
        label="操作"
        width="120px"
        >
        <template v-slot="{row}">
          <div class="actions">
        <i
          class="el-icon-view"
          @click="openDrawer('preview',row.id)"
        ></i>
        <i
          class="el-icon-edit-outline"
          @click="openDrawer('edit',row.id)"
        ></i>
        <i class="el-icon-delete" @click="del(row.id)"></i>
      </div>
        </template>
      </el-table-column>
      </el-table>
      <el-pagination
      background
      layout="prev, pager, next"
      :pageSize="pageSize"
      :total="total"
      @current-change="hChange"
      ></el-pagination>
    </el-card>
    <el-drawer
      :visible.sync="isShowDrawer"
      :before-close="handleClose"
      :title="drawerTitle"
      direction="rtl"
      size="50%"
    >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="标题" prop="stem">
        <el-input v-model="form.stem" placeholder="输入面经标题"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <quill-editor v-model="form.content" @blur="$refs.form.validateField('content')"></quill-editor>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addArticle">确认</el-button>
        <el-button @click="handleClose">取消</el-button>
      </el-form-item>
    </el-form>
    </el-drawer>
  </div>
</template>

<script>
import { createArticle, delArticle, getArticle } from '@/api/article'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { Message } from 'element-ui'
export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  data () {
    return {
      list: [],
      pageSize: 10,
      total: 0,
      current: 1,
      isShowDrawer: false,
      drawerType: '',
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [{ required: true, message: '请输入面经标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入面经内容', trigger: 'blur' }]
      }
    }
  },
  async created () {
    this.loadData()
  },
  methods: {
    async loadData () {
      const res = await getArticle({
        current: this.current,
        pageSize: this.pageSize
      })
      console.log(res)
      this.total = res.data.total
      this.list = res.data.rows
    },
    async del (id) {
      console.log(id)
      await delArticle({ id })
      Message.success('删除成功!')
      this.loadData()
    },
    hChange (current) {
      console.log(1)
      this.current = current
      this.loadData()
    },
    openDrawer (type, id) {
      console.log(type, id)
      this.isShowDrawer = true
      this.drawerType = type
    },
    handleClose () {
      // this.$refs.form.resetFileds()
      this.isShowDrawer = false
    },
    async addArticle () {
      // 发送axios请求将新增数据发送至服务器
      await createArticle(this.form)
      // 发送成功提醒
      Message.success('数据添加成功!')
      // 将页面重置为第一页方便查看新增内容
      this.current = 1
      // 重新渲染页面
      this.loadData()
      // 关闭弹框并清空表单
      this.handleClose()
    }
  },
  computed: {
    drawerTitle () {
      // 普通写法
      /* if (this.drawerType === 'add') {
        return '添加面经'
      } else if (this.drawerType === 'preview') {
        return '面经预览'
      } else {
        return '修改面经'
      } */
      // 高级数据字典写法
      const obj = {
        add: '添加面经',
        preview: '面经预览',
        edit: '修改面经'
      }
      return obj[this.drawerType]
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
